package com.ruoyi.edit.service.impl;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.CopyObjectRequest;
import com.aliyun.oss.model.CreateBucketRequest;
import com.aliyun.oss.model.PutObjectRequest;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.edit.domain.Ability;
import com.ruoyi.edit.mapper.AbilityMapper;
import com.ruoyi.edit.service.IAbilityService;
import com.ruoyi.edit.service.OssService;
import com.ruoyi.edit.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 能力指标Service业务层处理
 * 
 * @author qiufeng
 * @date 2022-03-10
 */
@Service
public class OssServiceImpl implements OssService
{
    //测试地址
    @Value("${oss.endpoint}")
    private String endpoint;
    @Value("${oss.accessKeyId}")
    private String accessKeyId;
    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${oss.bucketName}")
    private String bucketName;
    @Value("${oss.url}")
    public  String url;
    //测试地址
//    private String endpoint = "oss-cn-shanghai.aliyuncs.com";
//    private String accessKeyId = "LTAI5tRU1w4C1npBtMrTwyd8";
//    private String accessKeySecret = "N4PUolI5CRU6K380BnsqTNULnbzrF9";
//    private String bucketName ="dotflat-saas-res";
//    public  String url =  "https://saas.res.dotflat.cn/";
//    正式地址
//    private String endpoint = "oss-cn-beijing-internal.aliyuncs.com";
//    private String accessKeyId = "LTAI5tRCSWxwCtZ8qcKEgQM9";
//    private String accessKeySecret = "Fjzi0oHYYbgy4gYLnVgLwG6tCxPFG2";
//    private String bucketName ="xxw-res";
//    public  String url =  "https://res.xxwbaby.com/";

    public  String path = "teach/file/course/";

    public String aliyunOssUpload(String relativePath, MultipartFile file) {
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            if (!ossClient.doesBucketExist(bucketName)) {
                /*
                 * Create a new OSS bucket
                 */
                ossClient.createBucket(bucketName);
                CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
                ossClient.createBucket(createBucketRequest);
            }

            ossClient.putObject(new PutObjectRequest(bucketName, relativePath, file.getInputStream()));
            ossClient.setObjectAcl(bucketName, relativePath, CannedAccessControlList.PublicRead);
            ossClient.setObjectAcl(bucketName, relativePath, CannedAccessControlList.Default);

        } catch (OSSException oe) {

        } catch (ClientException ce) {

        } catch (IOException ie){

        } finally{
            ossClient.shutdown();
        }
        return relativePath;
    }

    public String aliyunOssUpload(String relativePath, File file) {
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        try {
            if (!ossClient.doesBucketExist(bucketName)) {
                /*
                 * Create a new OSS bucket
                 */
                ossClient.createBucket(bucketName);
                CreateBucketRequest createBucketRequest= new CreateBucketRequest(bucketName);
                createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
                ossClient.createBucket(createBucketRequest);
            }

            ossClient.putObject(new PutObjectRequest(bucketName, relativePath, file));
            ossClient.setObjectAcl(bucketName, relativePath, CannedAccessControlList.PublicRead);
            ossClient.setObjectAcl(bucketName, relativePath, CannedAccessControlList.Default);

        } catch (OSSException oe) {

        } catch (ClientException ce) {

        }finally{
            ossClient.shutdown();
        }
        return relativePath;
    }

    public void aliyunOssDelete(String relativePath) {
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        ossClient.deleteObject(bucketName, relativePath);
    }

    public void aliyunOssCopy() {
        OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
        CopyObjectRequest copyObjectRequest = new CopyObjectRequest("", "", "", "");
    }

    public static void main(String[] args) {
        String v = "2022-10-8 23:00:00";
        String v1 = "2022-10-9 23:00:01";
        Date date = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",v);
        System.out.println(birthdayDay(new Date(),date));

        System.out.println(~((8/2) - 1));

    }

    public static  Date addDay(Date date ,int day){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        // 把日期往后增加一天,整数  往后推,负数往前移动
        calendar.add(Calendar.DATE, day);
        // 这个时间就是日期往后推一天的结果
        return  calendar.getTime();
    }

    /**
     * 获取出生到现在多少天
     *
     * @param date  当前日期
     * @param date1 生日
     * @return
     */
    public static int birthdayDay(Date date, Date date1) {
        long betweenDays = (date.getTime() - date1.getTime()) / (1000L*3600L*24L);
        return (int) Math.abs(betweenDays);
    }
}
